package com.zjg.dao;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zjg.bean.Blog;
import com.zjg.dao.iface.BlogDao;
import com.zjg.db.utills.JdbcTemplate;

public class BlogDaoImp implements BlogDao {

	@Override
	public List<Blog> getAllBlog() {
		String sql="select b.*,bt.typename, bt.id from blog b  " + 
				"join blog_type b_t on b_t.blog_id=b.blog_id " + 
				"join blogtype bt on bt.id=b_t.blogtype_id ";
		List<Blog> list=JdbcTemplate.query(sql, (rs)->{
				List<Blog> list0=new ArrayList<>();
			try {
				while(rs.next()) {
					Timestamp timeStamp=rs.getTimestamp(4);
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Long time =timeStamp.getTime();
					Date sd = new Date(time);
					
					Blog blog=new Blog(rs.getInt(1),rs.getString(2),rs.getString(3),null,rs.getInt(5),rs.getInt(6));
					blog.setBlog_time(sd);
					blog.setType_name(rs.getString(8));
					blog.setType_id(rs.getString(9));
					list0.add(blog);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			
			return list0;
		},null);
		
		
		return list;
	}

	@Override
	public List<Map<String, Object>> getBlogTypeList() {
		String sql="select * from blogtype";
		 List<Map<String,Object>> list=JdbcTemplate.query(sql, (rs)->{
			 List<Map<String,Object>> list0=new ArrayList<>();
			 try {
				while(rs.next()) {
					 Map<String,Object> map=new HashMap<String,Object>();
					 map.put("typename", rs.getString(2));
					 map.put("typecode", rs.getString(3));
					 list0.add(map);
				 }
			} catch (SQLException e) {
				e.printStackTrace();
			}
			  return list0;
		 }, null);
		return list;
	}

	@Override
	public Blog getBlogById(String article_id) {
		String sql="select b.*,bt.typename, bt.id from blog b   " + 
				"        join blog_type b_t on b_t.blog_id=b.blog_id  " + 
				"        join blogtype bt on bt.id=b_t.blogtype_id where b.blog_id=?";
		Blog blog=JdbcTemplate.query(sql, (rs)->{
			Blog blog0=new Blog();
			try {
				while(rs.next()) {
					blog0.setBlog_id(rs.getInt(1));
					blog0.setBlog_name(rs.getString(2));
					blog0.setBlog_text(rs.getString(3));
					Timestamp timeStamp=rs.getTimestamp(4);
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Long time =timeStamp.getTime();
					Date sd = new Date(time);
					blog0.setStringtime(sdf.format(sd));
					blog0.setBlog_time(sd);
					blog0.setSkr(rs.getInt(5));
					blog0.setOrder_user_id(rs.getInt(6));
					blog0.setBlog_view_num(rs.getInt(7));
					blog0.setType_name(rs.getString(8));
					blog0.setType_id(rs.getString(9));
				
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			return blog0;
		}, new  Object[]{article_id});
		return blog;
	}

	@Override
	public int setArticle(Blog blog) {
		int rowNum=0;
		String sql="INSERT INTO BLOG (blog_name,blog_text,blog_skr,order_user_id,Blog_Views_Num) VALUES(?,?,0,?,0)";
		rowNum+=JdbcTemplate.update(sql,blog.getBlog_name(),blog.getBlog_text(),blog.getOrder_user_id());
		String sql0="insert into blog_type(blog_id,blogtype_id) values((select blog_id from blog where blog_name=? and order_user_id=?),?)";
		rowNum+=JdbcTemplate.update(sql0, blog.getBlog_name(),blog.getOrder_user_id(),blog.getOrder_user_id());
		return rowNum;
	}

}
